package com.cinyi.ormx.android.context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * 应用程序配置类：用于保存用户相关信息及设置
 * 
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppConfig {

	private final static String APP_CONFIG = "config";

	private Context mContext;
	private static AppConfig appConfig;

	private static SharedPreferences mSharedPreferences ;
	
	public static AppConfig getAppConfig(Context context) {
		if (appConfig == null) {
			appConfig = new AppConfig();
			appConfig.mContext = context;
			
			mSharedPreferences = context.getSharedPreferences(appConfig.getClass().getPackage().getName()+"_pref",Context.MODE_PRIVATE);
		}
		return appConfig;
	}

	/**
	 * 获取Preference设置
	 */
	public static SharedPreferences getSharedPreferences(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context);
	}

	public String get(String key) {
		Properties props = get();
		return (props != null) ? props.getProperty(key) : null;
	}

	public Properties get() {
			FileInputStream fis = null;
			Properties properties = new Properties();
			try {
				// 读取app_config目录下的config
				File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
				fis = new FileInputStream(dirConf.getPath() + File.separator
						+ APP_CONFIG);
				properties.load(fis);
			} catch (Exception e) {
			} finally {
				try {
					fis.close();
				} catch (Exception e) {
				}
			}
//		}
		return properties;
	}
	
	private void setProps(Properties p) {
		if(p != null)
			setProperties(p);
	}
	
	private void setProperties(Properties p) {
		FileOutputStream fos = null;
		try {
			// 把config建在(自定义)app_config的目录下
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			File conf = new File(dirConf, APP_CONFIG);
			fos = new FileOutputStream(conf);

			p.store(fos, null);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (Exception e) {
			}
		}
	}

	public void set(Properties ps) {
		Properties props = get();
		props.putAll(ps);
		setProps(props);
	}

	public void set(String key, String value) {
		if(key == null || value == null ){
			return ;
		}
		Properties props = get();
		
		props.setProperty(key, value);
		setProps(props);
	}

	public void remove(String... key) {
		Properties props = get();
		for (String k : key)
			props.remove(k);
		setProps(props);
	}
	
	public void removeAttribute(String key){
		mSharedPreferences.edit().remove(key).commit();
	}
	
	public void setAttribute(String key,String val){
		mSharedPreferences.edit().putString(key, val ).commit();
	}
	
	public void setAttribute(String key,int val){
		mSharedPreferences.edit().putString(key, val+"" ).commit();
	}
	
	public String getAttribute(String key,String defValue){
		return mSharedPreferences.getString(key, defValue) ;
	}
	
}
